package czk.concrent.juc.countDOwnLatch.practice;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 模拟和平精英游戏加载
 * @Author:ChenZhangKun
 * @Date: 2020/10/8 22:29
 */
public class PeaceEliteDemo {
    public static void main(String[] args) throws InterruptedException {
        // 定义四个
        CountDownLatch countDownLatch=new CountDownLatch(4);
        // 定义线程更池
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        // 定义随机数
        Random r=new Random();
        // 定义数组
        String[] all=new String[4];
        for (int i = 0; i < 4; i++) {
            int k=i;
            executorService.submit(()->{
                for (int j = 0; j <= 100; j++) {
                    try {
                        Thread.sleep(r.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    all[k]=j+"%";
                    System.out.print("\r"+ Arrays.toString(all));
                }
                countDownLatch.countDown();
            });
        }
        System.out.println("游戏加载中");
        countDownLatch.await();
        System.out.println();
        System.out.println("游戏加载完毕");
        executorService.shutdown();
    }
}
